package cz.proks.easyhelpdesk.gwt.ui;

import java.util.Date;
import java.util.List;

import com.google.gwt.cell.client.DateCell;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

import cz.proks.easyhelpdesk.api.model.Ticket;
import cz.proks.easyhelpdesk.gwt.HelpdeskMessages;
import cz.proks.easyhelpdesk.gwt.event.LeftArrowClickEvent;
import cz.proks.easyhelpdesk.gwt.event.RightArrowClickEvent;
import cz.proks.easyhelpdesk.gwt.keyprovider.KeyProvider;
import cz.proks.easyhelpdesk.gwt.ui.composite.RestrictedHelpdeskComposite;
import cz.proks.easyhelpdesk.gwt.ui.layout.HelpdeskMainLayoutPanel;
import cz.proks.easyhelpdesk.gwt.ui.resource.CellTableResources;

public class HelpdeskMainViewImpl extends RestrictedHelpdeskComposite implements HelpdeskMainView {

    private static HelpdeskMainViewImplUiBinder uiBinder = GWT.create(HelpdeskMainViewImplUiBinder.class);

    interface HelpdeskMainViewImplUiBinder extends UiBinder<Widget, HelpdeskMainViewImpl> {
    }
    
    private Presenter presenter;

    
    private CellTableResources cellResources = GWT.create(CellTableResources.class);
    
    private final KeyProvider<Ticket> ticketKeyProvider = new KeyProvider<Ticket>();
    private final ListDataProvider<Ticket> ticketDataProvider = new ListDataProvider<Ticket>(ticketKeyProvider);
    private final int ticketsForPage = 20;
    
    @UiField HelpdeskMessages i18n;
    @UiField(provided=true) CellTable<Ticket> ticketTable = new CellTable<Ticket>(ticketsForPage, cellResources, ticketKeyProvider);    
    @UiField HelpdeskMainLayoutPanel mainLayoutPanel;
    
    
    public HelpdeskMainViewImpl() {   
        initWidget(uiBinder.createAndBindUi(this));
        initTicketTable();
        
        addRightArrowClickHandler(this);
        addLeftArrowClickHandler(this);
    }
    
    @Override
    protected void initWidget(Widget widget) {     
        super.initWidget(widget);
    }

    @Override
    public void setPresenter(Presenter presenter) {
        this.presenter = presenter;
    }
    
    @Override
    protected HelpdeskMainLayoutPanel getRootPanel() {
        return mainLayoutPanel;
    }
    
    private Presenter getPresenter() {
        return presenter;
    }    

    @Override
    public List<Ticket> getTickets() {
        return ticketDataProvider.getList();
    }
                
    @Override
    public void onRightArrowClick(RightArrowClickEvent event) {
        if (presenter != null)
            presenter.showNextTicketPage();
    }

    @Override
    public void onLeftArrowClick(LeftArrowClickEvent event) {
        if (presenter != null)
            presenter.showPreviousTicketPage();
    }    

    @Override
    public int getTicketsByPage() {
        return ticketsForPage;
    }
    
    @Override
    public void onErase() {
        
    }

    private void initTicketTable() {
        ticketTable.setWidth("100%", true);
        final SingleSelectionModel<Ticket> selection = new SingleSelectionModel<Ticket>(ticketKeyProvider);
        selection.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
            
            @Override
            public void onSelectionChange(SelectionChangeEvent event) {
               getPresenter().showTicket(selection.getSelectedObject());                
            }
        });
        
        ticketTable.setSelectionModel(selection);
        ticketDataProvider.addDataDisplay(ticketTable);
        initTicketTableColumns();
    }
    
    private void initTicketTableColumns() {
        Column<Ticket, String> subjectColumn = new Column<Ticket, String>(new TextCell()) {
            @Override
            public String getValue(Ticket object) {
                return object.getSubject();
            }            
        };
        
        Column<Ticket, String> descriptionColumn = new Column<Ticket, String>(new TextCell()) {
            @Override
            public String getValue(Ticket object) {                
                String description = object.getDescription().replaceAll("\\<.*?>", "")
                                           .replaceAll("&nbsp;", "")
                                           .replaceAll("&amp;", "");
                return description;
            }            
        };
        
        Column<Ticket, Date> createdColumn = new Column<Ticket, Date>(new DateCell()) {
            @Override
            public Date getValue(Ticket object) {
                return object.getCreated();
            }            
        };
        
        ticketTable.addColumn(subjectColumn, i18n.ticketSubjectColumn());
        ticketTable.addColumn(descriptionColumn, i18n.ticketDescriptionColumn());
        ticketTable.addColumn(createdColumn, i18n.ticketCreatedColumn());
    }    
}
